package com.Telien.app.ui.main;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.Teilen.app.db.main.DBWrapper;
import com.Telien.app.Telien;
import com.Telien.app.request.object.DriverRequest;
import com.Telien.app.request.object.rideService;
import com.Telien.app.user.object.User;
import com.Telien.app.user.service.SessionManagementService;
import com.Telien.app.user.service.UserManagementService;

public class DriverRideRequestPanel extends JPanel implements ActionListener, TableModelListener{
	private JTable table;
	private	JScrollPane scrollPane;
	private User user;
	private static String columnNames[] = { "Username", "Phone Number", "Start Location","End Location","Start Time"};
	Object dataValues[][] = {
			{"", "", "", "", ""}
	};
	private JPopupMenu popupMenu;
	private JMenuItem acceptItem;
	private JMenuItem declineItem;
	private JMenuItem refreshItem;
	DBWrapper db = new DBWrapper();
	SessionManagementService sms;
	UserManagementService ums;
	ArrayList<rideService> driverOwnedSessions = new ArrayList<rideService>();
	//ArrayList<DriverRequest> associatedRequests = new ArrayList<DriverRequest>();
	
	public DriverRideRequestPanel(User user) {
		this.user = user;
		
		DBWrapper db = new DBWrapper();
		sms = new SessionManagementService();
		ums = new UserManagementService();
		try {
			driverOwnedSessions = sms.getSessionsWhereDriverId(Integer.parseInt(user.getUserUUID()));
			
		}
		catch (NumberFormatException e) {
			System.out.println("Number format exception at DriverRideRequestPanel. user.getUserUUID can't be parsed to an int");
			System.out.println(user.getUserUUID());
		}
		
		ums.loadUsersFromDb();
		dataValues = new Object [driverOwnedSessions.size()][5];
		if (!driverOwnedSessions.isEmpty()) {
			for (int numSessions = 0; numSessions < driverOwnedSessions.size(); numSessions++) {
				System.out.println(driverOwnedSessions.get(numSessions).getrequestUserID() + "");
				User rider = ums.getUserObjFromUUID(driverOwnedSessions.get(numSessions).getrequestUserID() + "");
				
				dataValues[numSessions][0] = rider.getUserName();
				dataValues[numSessions][1] = rider.getPhoneNumber();
				dataValues[numSessions][2] = driverOwnedSessions.get(numSessions).getRequest().getStartCord();
				dataValues[numSessions][3] = driverOwnedSessions.get(numSessions).getRequest().getEndCord();
				dataValues[numSessions][4] = driverOwnedSessions.get(numSessions).getsessionDate();
			}
		}
		
		// something
		
		this.initialize();
	}
		
	private void initialize() {			
		//setLayout(experimentLayout);
		setLayout(new BorderLayout());
		table = new JTable(dataValues,columnNames);
		scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
		popupMenu = new JPopupMenu();
		acceptItem = new JMenuItem("Accept");
		declineItem = new JMenuItem("Decline");
		refreshItem = new JMenuItem("Refresh");
		acceptItem.addActionListener(this);
		declineItem.addActionListener(this);
		refreshItem.addActionListener(this);
		popupMenu.add(acceptItem);
		popupMenu.add(declineItem);
		popupMenu.add(refreshItem);
		table.setComponentPopupMenu(popupMenu);
		
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		String name = e.getActionCommand();
		if (name.equals("Accept")) {
			System.out.println("Accept");
			//TODO
			//Changes Activity to true
			if (table.getSelectedRow() != -1) {
				int rideSessionID = driverOwnedSessions.get(table.getSelectedRow()).getrideSessionID();
				db.updateRideSessionActivity(true, rideSessionID); //how do i get ridesessionID
				Telien.getMainFrame().reloadMyRequests();
				Telien.getMainFrame().refreshDriverRideRequestPanel();
			}
			
		}
		if (name.equals("Decline")) {
			System.out.println("Decline");
			//TODO
			//Removes request from database
			if (table.getSelectedRow() != -1) {
				int rideSessionID = driverOwnedSessions.get(table.getSelectedRow()).getrideSessionID();
				db.updateRideSessionActivity(false, rideSessionID); //how do i get ridesessionID
				Telien.getMainFrame().reloadMyRequests();
				Telien.getMainFrame().refreshDriverRideRequestPanel();
			}
		}
		if (name.equals("Refresh")) {
			System.out.println("Refresh");
			Telien.getMainFrame().refreshDriverRideRequestPanel();
		}
	}

	public void tableChanged(TableModelEvent arg0) {
		// TODO Auto-generated method stub	
	}
}
